import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { Router ,ActivatedRoute} from '@angular/router'; //导入router服务
import { CommonService } from '../../service/common.service'
import { StorageService } from "../../service/storage.service";
import { EventService } from '../../service/event.service'

@Component({
  selector: 'app-bindwallet',
  templateUrl: './bindwallet.page.html',
  styleUrls: ['./bindwallet.page.scss'],
})
export class BindwalletPage implements OnInit {

  public contents:string = '';
  public show:boolean = false;
  public expression:boolean = false;
  public password:string;

  public name:string = ''
  
  
  constructor(private common:CommonService,private activatedRoute:ActivatedRoute,
    private storage:StorageService,private router:Router,private eventService:EventService) { }

  ngOnInit() {
    //接受路由传值过来的图片路径
    this.activatedRoute.queryParams.subscribe((data: any) => {
      this.name = data.name
    })
  }

  confirm(){
    if(this.contents == ''){
      this.common.presentAlert("请输入要绑定的钱包地址")
      return
    }
    this.password = null
    this.show = true
    var api = '/tbr/getVerifyWalletAddress'
    var params = {walletAddress: this.contents,addressType:this.name}
    // debugger
    this.common.post(api,params,false).then((response:any)=>{  
        if(response.code == '200'){
          console.log(response)
          if(response.data == 2){
            this.common.presentToast("验证钱包地址成功")
            this.expression = true
          }else{
            this.common.presentToast("验证地址失败")
          }
        }
        this.show = false
    })
  }


  events(){
    // console.log(this.eventService)
    this.eventService.event.emit('userstate');
    this.eventService.event.emit('useraction');
    console.log(this.eventService)
  }


  confirmB(){
    if(this.password.length !== 6){
      this.common.presentToast("请输入6位数密码")
      return
    }
    this.expression = false

    var api = '/tbr/wallet/saveWallet'
    var params = {
        "coinType":this.name,
        "address":this.contents,
        "paymentCode":this.password,
    }
    
    this.common.post(api,params,false).then((res:any)=>{
      console.log(res)
      if(res.code == '200'){
        // this.events()
        this.common.presentToast(res.msg)
        this.router.navigateByUrl('tabs/tab4')
      } 
    })
  }

}

